<template>
  <div class="app-container">
    <div class="mod-config">
      <el-form size="small" :inline="true" :model="dataForm">
        <el-form-item>
          <el-button type="primary" @click="addOrUpdateHandle()" v-hasPermi="['table:area:save']">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table :highlight-current-row="true" @row-click="query" :data="dataList" border v-loading="dataListLoading"
                style="width: 100%;"
      >
        <el-table-column prop="areaName" header-align="center" label="区域名称"/>
        <el-table-column prop="status" header-align="center" label="区域状态">
          <template v-slot="{ row, column, $index }">
            <el-tag type="success" v-if="row.status === 1">启用</el-tag>
            <el-tag type="danger" v-if="row.status === 0">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" header-align="center" label="备注"/>
        <el-table-column fixed="right" header-align="center" width="150" label="操作">
          <template v-slot="{ row, column, $index }">
            <el-button v-if="row.status === 1" type="text" size="small"
                       @click.stop="updateAreaStatus(row.areaId, 0)"
                       v-hasPermi="['table:area:update']"
            >停用
            </el-button>
            <el-button v-if="row.status === 0" type="text" size="small"
                       @click.stop="updateAreaStatus(row.areaId, 1)"
                       v-hasPermi="['table:area:update']"
            >启用
            </el-button>

            <el-button type="text" size="small" @click.stop="addOrUpdateHandle(row.areaId)"
                       v-hasPermi="['table:area:update']"
            >修改
            </el-button>
            <el-button type="text" size="small" @click.stop="deleteHandle(row.areaId)"
                       v-hasPermi="['table:area:delete']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="totalPage > 0"
        :total="totalPage"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getDataList()"
      />
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"/>
    </div>
    <div style="float: left;width: 60%;padding-left: 20px;">
      <Tables ref="TablesRef"/>
    </div>
  </div>
</template>

<script>
import AddOrUpdate from './components/tableAreaAddOrUpdate.vue'
import Tables from './diningTable.vue'
import { deleteTableAreaById, pageTableArea, updateStatus } from '@/api/table/tableArea'

export default {
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  components: {
    AddOrUpdate,
    Tables
  },
  created() {
    this.getDataList()
  },
  methods: {
    query(row) {
      this.$refs.TablesRef.getDataList(row.areaId)
    },

    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      pageTableArea(this.queryParams).then(response => {
        this.dataList = response.rows
        this.totalPage = response.total
        this.dataListLoading = false
      })
    },
    /**
     * 修改桌台区域状态
     * @param areaId 桌台区域id
     * @param status 状态  0 关闭 1 开启
     */
    updateAreaStatus(areaId, status) {
      this.$modal.confirm(`确定${status === 0 ? '停用' : '启用'}区域吗, 是否继续?`).then(() => {
        updateStatus({ areaId: areaId, status: status }).then((response) => this.$modal.msgSuccess(response.msg))
      }).finally(() => this.getDataList())
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(areaId) {
      this.$modal.confirm(`确定对[id=${areaId}]进行删除操作?`).then(() => {
        deleteTableAreaById(areaId).then(response => {
          this.$modal.msgSuccess(response.msg)
          this.getDataList()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .mod-config {
    float: left;
    width: 40%;
    padding-left: 20px;
  }
}
</style>
